/**
 * Created by admin on 2017/9/12.
 */
define([
    "views/modules/popup",
    "views/modules/form_search",
    "helpers/network",
    "views/modules/form",
    "views/modules/table",
    "views/modules/pager",
    "models/products"
], function (PopupView, FormSearchView, Network, CommonForm, CommonTable, CommonPager, Products) {
    var TYPE = "GoodsCategory";
    var id = "";
    var keywordsId = id + "keywordsId";
    var windowsId = id + "window";
    var formId = id + "form";
    var searchFormId = id + "searchForm";
    var tableId = id + "table";
    var pagerId = id + "pager";
    var totalCountsId = id + "totalCounts";
    var pageIndex = 1, pageCounts;

    var onTableReady = function (page) {
        // page = page || 1;
        Products.fetchGoodsCategories(function (ret) {
            $$(tableId).clearAll();
            $$(tableId).parse(ret.data.content);
            CommonPager.refreshPager(page, ret.data.totalPages);
            $$(totalCountsId).setValue(ret.data.totalElements + "条数据, 共" + ret.data.totalPages + "页");
        })
        // pageIndex = page ? page : 1
    };

    var onSubmit = function (itemId) {
        if (!$$(formId).validate()) {
            webix.message("信息填写有误, 请仔细录入...");
            return
        }
        var itemValues = $$(formId).getValues();
        itemValues.lastModifyTime = new Date();
        var obj = {
            data: {
                id: itemId || undefined,
                type: TYPE,
                data: itemValues,
                check: {
                    unique: ["value"],
                    // "ge":["balance:0"]
                }
            }
        };

        Network.ajax("/BizTransactor.save.command", obj, function (ret) {
            closeWindow();
            onTableReady();
        })
    };

    var onWindowReady = function (item) {
        // $$(formId).setValues(item.data);
    };

    var closeWindow = function () {
        if ($$(windowsId)) {
            $$(windowsId).close();
        }
    };

    var showWindow = function (title, item) {
        item = item || {data: {}};
        PopupView(windowsId, title, {
            rows: [
                {
                    cols: [
                        {
                            view: "scrollview", scroll: "y", paddingY: 5, paddingX: 10,
                            body: {
                                rows: [
                                    {
                                        view: "form", borderless: true, id: formId,
                                        rows: [
                                            {
                                                label: "分类名称", name: "value", view: "text",
                                                required: true, value: item.data.value || ""
                                            },

                                            {
                                                label: "排序", name: "seq", view: "text",
                                                required: true, value: item.data.seq || ""
                                            },
                                            // {
                                            //     label: "分类编码", name: "code", view: "text",
                                            //     value: item.data.code || ""
                                            // },
                                            // CommonForm.genSingleImgUploader("thumbnail", "缩略图", item.data["thumbnail"], true),
                                            {
                                                label: "备注", name: "remark", view: "text",
                                                value: item.data.remark || ""
                                            }
                                        ]
                                    }]
                            }
                        }
                    ]
                },
                CommonForm.genSubmitButton(function () {
                    onSubmit(item.id)
                })
            ]
        }, 500, 550, function () {
            onWindowReady(item)
        }, closeWindow);
    };

    var genOperationFormUI = function () {
        return {
            view: "form", css: "toolbar", paddingY: 5, paddingX: 10,
            cols: [
                {view: "label", label: "操作"},
                {
                    view: "button", label: "添加", width: 90,
                    // hidden: Authority.hasNoAuth("GOODS_ADD"),
                    click: function () {
                        showWindow("添加")
                    }
                },
                {
                    view: "button", label: "修改", width: 90,
                    // hidden: Authority.hasNoAuth("GOODS_MODIFY"),
                    click: function () {
                        var item = $$(tableId).getSelectedItem();
                        if (item) showWindow("修改", item)
                    }
                },
                {
                    view: "button", label: "隐藏/显示", width: 90,
                    // hidden: Authority.hasNoAuth("GOODS_MODIFY"),
                    click: function () {
                        var item = $$(tableId).getSelectedItem();
                        // if (item) showWindow("修改", item)
                    }
                }
            ]
        }
    };

    var genTreeTableUI = function () {
        return {
            id: tableId, view: "datatable", resizeColumn: true, ready: onTableReady,
            select: "row", data: [],
            complexData: true,
            columns: [
                {
                    id: "id", header: "id"
                },
                {
                    id: "value", header: "分类名称", width: 300,
                    template: "#data.value#"
                },
                {
                    id: "value", header: "序号(越大越靠前展示)", width: 300,
                    template: "#data.seq#"
                },
                {
                    id: "status", header: "状态", width: 200
                },
                {
                    id: "remark", header: "备注", width: 200, fillspace: true,
                    template: "#data.remark#"
                },
                // CommonTable.genLastModifyTimeColumn()
            ]
        }
    };

    var genFooterUI = function () {
        return {
            cols: [
                CommonPager.genPagerUI(pagerId, onTableReady),
                CommonPager.genTotalCountsUI(totalCountsId), {}
            ]
        }
    };

    return {
        $ui: {
            type: "clean", padding: 10,
            rows: [
                genOperationFormUI(),
                genTreeTableUI(),
                genFooterUI()
            ]
        }
    };

});